package com.anzhen.interview.concurrent;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorTest {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 50, 100,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(2), new ThreadFactoryBuilder().setNameFormat("threadPoolExecutor-%d").build());

        for (int i = 0; i < 30; i++) {
            TimeUnit.SECONDS.sleep(1L);
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("i am thread:" + Thread.currentThread().getName());
                    try {
                        TimeUnit.SECONDS.sleep(200000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
